import json

from ..model.UserV import UserV
from . import db
# 查询
def getUserV(id=None):
    sql = ''
    if not id:
        # 查询所有结果
        sql = 'select * from userView;'
        rows = db.query(sql)
        users = []
        for row in rows:
            item = UserV(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
            users.append(item.toDic())
        print('getuser')
        return json.dumps(users)
    else:
        # 查询单个结果
        sql = f"select * from userView where id={id};"
        # 读取记录
        row = db.query(sql).fetchone()
        # 如果查询结果为空
        if not row:
            return "null"
        item = UserV(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
        return json.dumps(item.toDic())
